﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Manageweb_ManageLink_ManageTextLink : System.Web.UI.Page
{
    MODEL.Link ML = new MODEL.Link();
    MODEL.Link _ML = new MODEL.Link();

    BLL.Link BL = new BLL.Link();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        LB_ManageTextLink.Text = "";

        TABLE_Add_Mod.Visible = false;

        ClickInit();

    }


    #region 按钮事件注册 [初始化]
    protected void ClickInit()
    {
        try
        {
            if (LB_ID.Text == "")
            {
                Btn_Submit.Click += new EventHandler(Btn_Add_Click);

                LB_TableTitle.Text = "添加链接";
            }
            else
            {
                Btn_Submit.Click += new EventHandler(Btn_Mod_Click);

                LB_TableTitle.Text = "修改链接";
            }
        }
        catch (Exception ex)
        {
            LB_ManageTextLink.Text = ex.Message;
        }
    }
    #endregion


    #region 添加修改表格数据 [初始化]
    protected void PageDataInit()
    {
        try
        {
            if (LB_ID.Text == "")
            {
                TB_Title.Text = "";

                TB_URL.Text = "";

                CB_State.Checked = true;
            }
            else
            {
                ML.Link_ID = Convert.ToInt32(LB_ID.Text);

                _ML = BL.GetDataByID(ML);

                TB_Title.Text = _ML.Link_Title;

                TB_URL.Text = _ML.Link_URL;

                CB_State.Checked = _ML.Link_State;
            }
        }
        catch (Exception ex)
        {
            LB_ManageTextLink.Text = ex.Message;
        }
    }
    #endregion


    #region 链接状态 [格式化]
    protected string FormatState(object _State)
    {
        bool LinkState = Convert.ToBoolean(_State);

        if (LinkState)
        {
            return "已启用";
        }
        else
        {
            return "已锁定";
        }
    }
    #endregion


    #region 添加链接 [GridView] [Command]
    protected void LBtn_Add_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = "";

            PageDataInit();
        }
        catch (Exception ex)
        {
            LB_ManageTextLink.Text = ex.Message;
        }
    }
    #endregion
    #region 修改链接 [GridView] [Command]
    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = e.CommandArgument.ToString();

            PageDataInit();

            PageDataInit();
        }
        catch (Exception ex)
        {
            LB_ManageTextLink.Text = ex.Message;
        }
    }
    #endregion
    #region 删除链接 [GridView] [Command]
    protected void LBtn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            ML.Link_ID = Convert.ToInt32(e.CommandArgument);

            BL.Del(ML);

            GV_TextLinkList.DataBind();

            LB_ManageTextLink.Text = "删除成功";
        }
        catch (Exception ex)
        {
            LB_ManageTextLink.Text = ex.Message;
        }
    }
    #endregion
    #region 切换链接状态 [GridView] [Command]
    protected void Btn_State_Command(object sender, CommandEventArgs e)
    {
        try
        {
            ML.Link_ID = Convert.ToInt32(e.CommandArgument);

            BL.ModState(ML);

            GV_TextLinkList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageTextLink.Text = ex.Message;
        }
    }
    #endregion


    #region 添加链接 [Click]
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            ML.Link_Title = TB_Title.Text;
            ML.Link_Image = "";
            ML.Link_URL = TB_URL.Text;
            ML.Link_State = CB_State.Checked;
            ML.Link_DateTime = DateTime.Now;

            BL.Add(ML);

            GV_TextLinkList.DataBind();

            LB_ManageTextLink.Text = "添加成功";
        }
        catch (Exception ex)
        {
            LB_ManageTextLink.Text = ex.Message;
        }
    }
    #endregion


    #region 修改链接 [Click]
    protected void Btn_Mod_Click(object sender, EventArgs e)
    {
        try
        {
            ML.Link_Title = TB_Title.Text; 
            ML.Link_URL = TB_URL.Text;
            ML.Link_State = CB_State.Checked;
            ML.Link_ID = Convert.ToInt32(LB_ID.Text);

            BL.Mod(ML);

            LB_ManageTextLink.Text = "修改成功";

            GV_TextLinkList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageTextLink.Text = ex.Message;
        }
    }
    #endregion
    
}
